package mysql_connect;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Package: mysql_connect
 * @ClassName: MysqlConnectTest
 * @Author: x2606
 * @CreateTime: 2022/3/13 14:15
 * @Description: mysql连接测试
 */
@SuppressWarnings("all")
public class MysqlConnectTest {
    public static void main(String[] args) {
        //加载类，连接数据库
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        //创建商品表goods，名字，价格
        //添加两条数据
        //删除表
        String sql="create table goods (id int,name varchar(32),price double,introduce text)";
        //得到statement对象，把sql语句发送给mysql执行
        try {
            Statement statement = connection.createStatement();
            statement.executeUpdate(sql);

            statement.close();
            connection.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        System.out.println("成功！");

    }
}
